package com.ruoyi.ftrl.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ftrl.domain.BuildingInfo;
import com.ruoyi.ftrl.mapper.BuildingInfoMapper;
import com.ruoyi.ftrl.mapper.CommunityInfoMapper;
import com.ruoyi.ftrl.service.IBuildingInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 楼栋信息Service业务层处理
 * 
 * @author dihang
 * @date 2024-07-08
 */
@Service
public class BuildingInfoServiceImpl implements IBuildingInfoService 
{
    @Autowired
    private BuildingInfoMapper buildingInfoMapper;
    @Autowired
    private CommunityInfoMapper communityInfoMapper;

    /**
     * 查询楼栋信息
     * 
     * @param id 楼栋信息主键
     * @return 楼栋信息
     */
    @Override
    public BuildingInfo selectBuildingInfoById(Long id)
    {
        return buildingInfoMapper.selectBuildingInfoById(id);
    }

    /**
     * 查询楼栋信息列表
     * 
     * @param buildingInfo 楼栋信息
     * @return 楼栋信息
     */
    @Override
    public List<BuildingInfo> selectBuildingInfoList(BuildingInfo buildingInfo)
    {
        return buildingInfoMapper.selectBuildingInfoList(buildingInfo);
    }

    /**
     * 新增楼栋信息
     * 
     * @param buildingInfo 楼栋信息
     * @return 结果
     */
    @Override
    public int insertBuildingInfo(BuildingInfo buildingInfo)
    {
        buildingInfo.setCreateTime(DateUtils.getNowDate());
        buildingInfo.setCreateBy(SecurityUtils.getUsername());
        BuildingInfo buildingInfo1=buildingInfoMapper.selectBuildingInfoByName(buildingInfo.getName());
        if (buildingInfo1!=null){
            throw new ServiceException("该楼栋已经存在，不能进行重复添加！");
        }
        //添加所属小区时将该小区的id进行存储，形成二级目录
        buildingInfo.setCommunityId(communityInfoMapper.selectCommunityInfoByName(buildingInfo.getCommunityName()).getId());
        return buildingInfoMapper.insertBuildingInfo(buildingInfo);
    }

    /**
     * 修改楼栋信息
     * 
     * @param buildingInfo 楼栋信息
     * @return 结果
     */
    @Override
    public int updateBuildingInfo(BuildingInfo buildingInfo)
    {
        buildingInfo.setUpdateTime(DateUtils.getNowDate());
        buildingInfo.setUpdateBy(SecurityUtils.getUsername());
        return buildingInfoMapper.updateBuildingInfo(buildingInfo);
    }

    /**
     * 批量删除楼栋信息
     * 
     * @param ids 需要删除的楼栋信息主键
     * @return 结果
     */
    @Override
    public int deleteBuildingInfoByIds(Long[] ids)
    {
        return buildingInfoMapper.deleteBuildingInfoByIds(ids);
    }

    /**
     * 删除楼栋信息信息
     * 
     * @param id 楼栋信息主键
     * @return 结果
     */
    @Override
    public int deleteBuildingInfoById(Long id)
    {
        return buildingInfoMapper.deleteBuildingInfoById(id);
    }

    /**
     * 根据小区id获取楼栋集合
     * @param id
     * @return
     */
    @Override
    public List<BuildingInfo> selectBuildingInfoByParentId(Long id) {
        return buildingInfoMapper.selectBuildingInfoByParentId(id);
    }
}
